From a75209f62982f7218f73b9b4fd9b705e19f5f94a Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= Date: Mon, 10 Nov 2025 17:36:59 +0100 Subject: [PATCH] blobmsg: refactor blobmsg_cast_u64/s64 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Instead of calling blobmsg_type() for each if/else block, just call it once and use it with a switch. Link: https://github.com/openwrt/libubox/pull/24 Signed-off-by: Álvaro Fernández Rojas --- blobmsg.h | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/blobmsg.h b/blobmsg.h index f2fc0d0..a34c4f7 100644 --- a/blobmsg.h +++ b/blobmsg.h @@ -305,32 +305,52 @@ static inline uint64_t blobmsg_get_u64(struct blob_attr *attr) static inline uint64_t blobmsg_cast_u64(struct blob_attr *attr) { - uint64_t tmp = 0; + const int type = blobmsg_type(attr); + uint64_t tmp; - if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + switch (type) { + case BLOBMSG_TYPE_INT64: tmp = blobmsg_get_u64(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32) + break; + case BLOBMSG_TYPE_INT32: tmp = blobmsg_get_u32(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16) + break; + case BLOBMSG_TYPE_INT16: tmp = blobmsg_get_u16(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) + break; + case BLOBMSG_TYPE_INT8: tmp = blobmsg_get_u8(attr); + break; + default: + tmp = 0; + break; + } return tmp; } static inline int64_t blobmsg_cast_s64(struct blob_attr *attr) { - int64_t tmp = 0; + const int type = blobmsg_type(attr); + int64_t tmp; - if (blobmsg_type(attr) == BLOBMSG_TYPE_INT64) + switch (type) { + case BLOBMSG_TYPE_INT64: tmp = blobmsg_get_u64(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT32) - tmp = (int32_t)blobmsg_get_u32(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT16) - tmp = (int16_t)blobmsg_get_u16(attr); - else if (blobmsg_type(attr) == BLOBMSG_TYPE_INT8) - tmp = (int8_t)blobmsg_get_u8(attr); + break; + case BLOBMSG_TYPE_INT32: + tmp = (int32_t) blobmsg_get_u32(attr); + break; + case BLOBMSG_TYPE_INT16: + tmp = (int16_t) blobmsg_get_u16(attr); + break; + case BLOBMSG_TYPE_INT8: + tmp = (int8_t) blobmsg_get_u8(attr); + break; + default: + tmp = 0; + break; + } return tmp; } -- 2.30.2